package com.mdp.workflow.ru.ctrl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mdp.core.entity.Result;
import com.mdp.core.err.BizException;
import com.mdp.core.query.QueryTools;
import com.mdp.core.utils.DateUtils;
import com.mdp.qx.HasRole;
import com.mdp.safe.client.entity.User;
import com.mdp.safe.client.utils.LoginUtils;
import com.mdp.swagger.ApiEntityParams;
import com.mdp.workflow.ru.entity.ProcinstParames;
import com.mdp.workflow.ru.service.ProcinstParamesService;
import com.mdp.workflow.ru.vo.PlanFinishTimeVo;
import com.mdp.workflow.ru.service.TaskServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value="/**/ru/procinstParames")
@Api(tags={"流程实例参数设置表-操作接口"})
public class ProcinstParamesController {
	
	static Logger logger =LoggerFactory.getLogger(ProcinstParamesController.class);
	
	@Autowired
	private ProcinstParamesService procinstParamesService;
	
	@Autowired
	private TaskServiceImpl taskService;

	@Value(value = "${mdp.platform-branch-id:platform-branch-001}")
	String platformBranchId;

	@ApiOperation( value = "流程实例参数设置表-查询列表",notes=" ")
	@ApiEntityParams(ProcinstParames.class)
	@ApiResponses({
		@ApiResponse(code = 200,response=ProcinstParames.class,message = "{tips:{isOk:true/false,msg:'成功/失败原因',tipscode:'错误码'},total:总记录数,data:[数据对象1,数据对象2,...]}")
	})
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public Result listProcinstParames(@ApiIgnore @RequestParam Map<String,Object> params){
		try {
			User user=LoginUtils.getCurrentUserInfo();
			QueryWrapper<ProcinstParames> qw = QueryTools.initQueryWrapper(ProcinstParames.class , params);
			IPage page=QueryTools.initPage(params);
			List<Map<String,Object>> datas = procinstParamesService.selectListMapByWhere(page,qw,params);
			return Result.ok("query-ok","查询成功").setData(datas).setTotal(page.getTotal());
		}catch (BizException e) {
			return Result.error(e);
		}catch (Exception e) {
			return Result.error(e);
		}
	}


	@HasRole
	@RequestMapping(value="/editPlanFinishTime")
	public Result editPlanFinishTime(@RequestBody PlanFinishTimeVo planFinishTimeVo) {
 
			User user=LoginUtils.getCurrentUserInfo();
			ProcinstParames p=new ProcinstParames();
			p.setId(planFinishTimeVo.getId());
			p.setPlanFinishTime(planFinishTimeVo.getPlanFinishTime());
			if(StringUtils.isEmpty(planFinishTimeVo.getId())) {
				return Result.error("参数编号id不能为空");
			}if(StringUtils.isEmpty(planFinishTimeVo.getProcInstId())) {
				return Result.error("流程实例编号procInstId不能为空");
			}if(StringUtils.isEmpty(planFinishTimeVo.getTaskId())) {
				return Result.error("任务编号taskId不能为空");
			}if(StringUtils.isEmpty(planFinishTimeVo.getPlanFinishTime())) {
				return Result.error("planFinishTime","计划完成时间不能为空"); 
			} else {
				procinstParamesService.updateSomeFieldByPk(p);
				if(StringUtils.isEmpty(planFinishTimeVo.getCommentMsg())) {
					if(planFinishTimeVo.getOldPlanFinishTime()==null) {
						planFinishTimeVo.setCommentMsg(user.getUsername()+"将计划完成时间由【未设置】改为"+ DateUtils.format(planFinishTimeVo.getPlanFinishTime(), "yyyy-MM-dd HH:mm:ss"));
					}else {
						planFinishTimeVo.setCommentMsg(user.getUsername()+"将计划完成时间由"+DateUtils.format(planFinishTimeVo.getOldPlanFinishTime(), "yyyy-MM-dd HH:mm:ss")+"改为"+DateUtils.format(planFinishTimeVo.getPlanFinishTime(), "yyyy-MM-dd HH:mm:ss"));
					}
				}
				taskService.addCommentMsg(user.getUserid(), planFinishTimeVo.getTaskId(), planFinishTimeVo.getProcInstId(), planFinishTimeVo.getCommentMsg());
 			} 
			return Result.ok();
			
	}
	@HasRole
	@RequestMapping(value="/editMainContext")
	public Result editMainContext(@RequestBody ProcinstParames procinstParames) {
 
			User user=LoginUtils.getCurrentUserInfo();
			if(StringUtils.isEmpty(procinstParames.getUserid())) {
				return Result.error("发起人编号userid必传");
			}else if(!user.getUserid().equals(procinstParames.getUserid())) {
				return Result.error("不是自己发起的流程不允许修改正文");
			}else {
				ProcinstParames p=new ProcinstParames();
				p.setId(procinstParames.getId());
				p.setMainContext(procinstParames.getMainContext());
				p.setLastTime(new Date());
				p.setLastUserid(user.getUserid());
				procinstParamesService.updateSomeFieldByPk(procinstParames);
			} 
			return Result.ok();
	}

}
